package ru.jexp.rcp.supply.handlers;

import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import ru.jexp.rcp.supply.editor.EditorInputBean;

public class OpenEditorHandler extends AbstractHandler
{

	public static final String ID = "ru.jexp.rcp.supply.handlers.openeditorhandler";

	public static final String KEY_ID_EDITOR = "key_id_editor";
	public static final String KEY_NAME_EDITOR = "key_name_editor";
	public static final String KEY_BEAN_EDITOR = "key_bean_editor";

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException
	{
		try
		{
			IWorkbenchWindow window = HandlerUtil
					.getActiveWorkbenchWindow(event);
			Event param = (Event) event.getTrigger();
			Map<String, Object> editorMap = (Map<String, Object>) param.data;
			String idEditor = (String) editorMap.get(KEY_ID_EDITOR);
			String nameEditor = (String) editorMap.get(KEY_NAME_EDITOR);
			Object bean = editorMap.get(KEY_BEAN_EDITOR);
			window.getActivePage().openEditor(
					new EditorInputBean(nameEditor, bean), idEditor, true,
					IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT);
		}
		catch (PartInitException e)
		{
			e.printStackTrace();
		}
		return null;
	}

}
